package com.lyn.module_custom_photo.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.lyn.module_custom_base.adapter.BaseAdapter;
import com.lyn.module_custom_base.dialog.BaseDialog;
import com.lyn.module_custom_photo.R;
import com.lyn.module_custom_photo.databinding.AdapterPhotoItemBinding;
import com.lyn.module_custom_photo.databinding.DialogSelectPhotoBinding;
import com.lyn.module_custom_photo.entity.ImageModel;
import com.lyn.module_custom_photo.utils.ImageCache;
import com.lyn.module_custom_photo.utils.PhotoUtil;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * created by lyn at 2021/10/27
 */
public class SelectPhotoDialog extends BaseDialog<DialogSelectPhotoBinding> {
    private Context context;
    private ImageCache imageCache;
    private BaseAdapter<ImageModel, AdapterPhotoItemBinding> adapter;

    public SelectPhotoDialog(@NonNull Context context) {
        super(context, R.style.common_dialog);
        this.context=context;
    }

    public SelectPhotoDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context=context;
    }

    @Override
    protected DialogSelectPhotoBinding getViewBinding() {
        return DialogSelectPhotoBinding.inflate(getLayoutInflater(), null, false);
    }

    @Override
    protected void bindData(DialogSelectPhotoBinding binding, Dialog dialog) {
        imageCache = new ImageCache();
        adapter = new BaseAdapter<ImageModel, AdapterPhotoItemBinding>() {
            @Override
            protected AdapterPhotoItemBinding getViewBinding(ViewGroup parent) {
                return AdapterPhotoItemBinding.inflate(getLayoutInflater(), null, false);
            }

            @Override
            protected void bindData(AdapterPhotoItemBinding adapterPhotoItemBinding, ImageModel imageModel, int position) {
                loadImage(adapterPhotoItemBinding.photo, imageModel.getPath());
            }

            @Override
            protected void bindListener(AdapterPhotoItemBinding adapterPhotoItemBinding, ImageModel imageModel, int position) {

            }
        };
        List<ImageModel> imageModelList = PhotoUtil.getInstance().getImages(context);
        List<ImageModel> imageModelList2=new ArrayList<>();
        int size=imageModelList.size();
        for (int i = 0; i <10; i++) {
            ImageModel imageModel = imageModelList.get(i);
            imageModelList2.add(imageModel);
        }
        adapter.setData(imageModelList2);
        binding.photoGridView.setAdapter(adapter);
    }

    @Override
    protected void bindListener(DialogSelectPhotoBinding binding, Dialog dialog) {

    }

    @Override
    protected boolean getDispatchKeyEvent() {
        return false;
    }

    @Override
    protected boolean customLengthAndWidth() {
        return true;
    }


    private void loadImage(ImageView imageView, String path) {
        LoadPhoto loadPhoto = new LoadPhoto(imageView, path);
        loadPhoto.execute();
    }


    private class LoadPhoto extends AsyncTask<Void, Integer, Bitmap> {

        private WeakReference<ImageView> photo;
        private String path;

        public LoadPhoto(ImageView photo, String path) {
            this.photo = new WeakReference<ImageView>(photo);
            this.path = path;
        }

        @Override
        protected Bitmap doInBackground(Void... voids) {
            Bitmap bitmapFromMemCache = imageCache.getBitmapFromMemCache(path);
            if (bitmapFromMemCache == null) {
                Bitmap bitmap = BitmapFactory.decodeFile(path);
                if (bitmap == null) {
                    return null;
                }
                Bitmap bitmap1 = PhotoUtil.getInstance().decodeBitmapFromDisk(path, 640,640);
                if (bitmap1 != null) {
                    imageCache.addBitmapToMemCache(path, bitmap1);
                }
                return bitmap1;
            } else {
                return bitmapFromMemCache;
            }

        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if (bitmap != null) {
                photo.get().setImageBitmap(bitmap);
            }
        }
    }

}
